package com.lancoo.examview.utils;

import android.content.Context;
import android.util.Log;

import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.QuesAudio;
import com.lancoo.answer.model.bean.Type;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by pengmi on 2022/2/24 14:58
 * Description:
 */
public class ConvertStremUtils {

    public static String convertStremToString(Context mContext,String dataAsset){

        InputStream inputStream;
        try{
            inputStream=mContext.getAssets().open(dataAsset);
            String s=null;
            try{
                Scanner scanner=new Scanner(inputStream,"UTF-8").useDelimiter("\\A");
                if(scanner.hasNext()){
                    s=scanner.next();
                }
                inputStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            inputStream.close();
            return s;
        }catch (Exception e){
            e.printStackTrace();
            Log.e("eee","Exception"+e.toString());
        }
        return "";
    }

    public static void reBuildTypeList(List<Type> typeList){
        for (Type type:typeList){
            List<Ques> quesList = type.getQuesList();
            Ques ques0 = quesList.get(0);
            List<QuesAudio> audioList = ques0.getAudioList();
            if(audioList==null||audioList.size()==0){
                continue;
            }
            int quesIndex=0;
            for (Ques ques:type.getQuesList()){
                List<QuesAudio> audioList1 = ques.getAudioList();
                List<QuesAudio> newAudioList =new ArrayList<>();
                QuesAudio leardAudio=new QuesAudio();
                leardAudio.setPath("https://lgresv57.obs.cn-south-1.myhuaweicloud.com/lgRs/da798294095f45d59eda503497d8fb7d/f4ca3ce39b6584eb56c83d890b261d7c.mp3");
                leardAudio.setType(0);
                if(quesIndex==0){
                    newAudioList.add(leardAudio);
                }
                newAudioList.add(addSubTimeAuio());
                if(audioList1.size()==1){//只有一个就直接加作答时长
                    newAudioList.add(audioList1.get(0));
                    QuesAudio answerQues=new QuesAudio();
                    answerQues.setType(2);
                    answerQues.setDuration(5);
                    answerQues.setPath("");
                    newAudioList.add(answerQues);

                }else{//如果有多个就从第二个开始加作答时长
                    for (int i = 0; i < audioList1.size(); i++) {
                        if(i>0){
                            QuesAudio quesAudio=audioList1.get(i);
                            quesAudio.setChildIndex(i-1);
                            newAudioList.add(quesAudio);
                            QuesAudio answerQues=new QuesAudio();
                            answerQues.setType(2);
                            answerQues.setChildIndex(i-1);
                            answerQues.setDuration(5);
                            answerQues.setPath("");
                            newAudioList.add(answerQues);
                        }else{
                            newAudioList.add(audioList1.get(i));
                            newAudioList.add(addSubTimeAuio());
                        }
                    }
                }


                ques.setAudioList(newAudioList);
                quesIndex++;
            }


        }
    }

    public static QuesAudio addSubTimeAuio(){
        QuesAudio quesAudio=new QuesAudio();
        quesAudio.setType(1);
        quesAudio.setDuration(3);
        return quesAudio;
    }

}

